package zhieasy.cn.service.initiation.impl;

import org.springframework.stereotype.Service;
import zhieasy.cn.model.common.FreshManId;
import zhieasy.cn.model.common.StudentUser;
import zhieasy.cn.model.common.WechatUser;
import zhieasy.cn.persistence.mapper.common.FreshmanIdMapper;
import zhieasy.cn.persistence.mapper.common.StudentUserMapper;
import zhieasy.cn.service.initiation.StudentService;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author sofency
 * @date 2020/8/8 14:57
 * @package IntelliJ IDEA
 * @description
 */
@Service
public class StudentServiceImpl implements StudentService {

    @Resource
    private StudentUserMapper studentUserMapper;
    @Resource
    private FreshmanIdMapper freshmanIdMapper;
    /**
     * 插入用户的信息
     * @param id
     * @return
     */
    @Override
    public Boolean register(String id) {
        FreshManId freshManId = freshmanIdMapper.selectById(id);//查找导入的数据中是否有用户的信息
        if(!Objects.isNull(freshManId)){
            String openId= WechatUser.getWechatUserOpenIdFromSecurityContext();
            StudentUser studentUser = new StudentUser();//注册用户信息
            studentUser.setOpenId(openId);
            studentUser.setCollegeId(freshManId.getCollageId());
            studentUser.setMajorId(freshManId.getMajorId());
            studentUser.setGradeId(freshManId.getMajorClass());//设置班级
            studentUser.setStudentCredent("null");
            studentUser.setUpdateTime(System.currentTimeMillis());
            studentUser.setCreateTime(System.currentTimeMillis());
            studentUser.setStudentEmail("");
            studentUser.setStudentName(freshManId.getName());
            int insert = studentUserMapper.insert(studentUser);
            return insert > 0;
        }else{
            return false;
        }
    }

    /**
     * 用户的表中是否有用户的消息
     * @param openId
     * @return
     */
    @Override
    public Boolean isHaveInfo(String openId) {
        StudentUser studentUser = studentUserMapper.selectById(openId);
        return studentUser != null;
    }
}
